#include "newwidget.h"
#include "QHBoxLayout"

/* @brief SpinSlider: Create one widget in parent widget
 * @param parent: The parent widget of this spinSlider
 * @author
 */
SpinSlider::SpinSlider(QWidget *parent):QWidget(parent)
{
    QLayout* lay;

    hlay = new QHBoxLayout;
    slider = new QSlider;
    spin = new QDoubleSpinBox;
    wid = new QWidget;
    lab = new QLabel;

    lab->setText("项目");
    slider->setOrientation(Qt::Horizontal);

    wid->setLayout(hlay);
    hlay->addWidget(lab);
    hlay->addWidget(slider);
    hlay->addWidget(spin);


    if(parent->layout())
    {
        lay = parent->layout();
        lay->addWidget(wid);
    }
    else
    {
        lay = new QHBoxLayout;
        lay->addWidget(wid);
    }
    slider->setValue(50);
    spin->setValue(50);
    connect(slider,&QSlider::valueChanged,this,[=](int value){valueChanged(value);});
    connect(spin,SIGNAL(valueChanged(double)),this,SLOT(valueChanged(double)));

    valueChanged(50);
}

/* @brief setMinMax: Set the max and min value
 * @param min: minimum
 * @param max: maximum
 * @author GDTR
 */
void SpinSlider::setMinMax(double min, double max)
{

    minimum = min;
    maximum = max;
    slider->setMinimum(min);
    spin->setMinimum(min);
    slider->setMaximum(max);
    spin->setMaximum(max);
}

/* @brief valueChanged:when the value of slider or spin changed,this slot will be called
 * @param value: The value changed
 * @return no
 * @author GDTR
 */
void SpinSlider::valueChanged(double value)
{
    if(value >= minimum && value <= maximum)
    {
        slider->setValue(value);
        spin->setValue(value);
    }
    emit valueChanging(value);
}


